package com.longxi.tianjintianbao.ui.activity.houseinspection;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/12/17
 */
public class CheckInFinishSaveActivity extends BaseActivity {

    @BindView(R.id.etOtherexplain)
    EditText etOtherexplain;
    @BindView(R.id.etHygieneexplain)
    EditText etHygieneexplain;
    @BindView(R.id.btnLogin)
    Button btnLogin;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_checkin_finish;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("入住验房完成保存");
        setTvLeft("返回");
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initListener() {
        super.initListener();
        btnLogin.setOnClickListener(v -> checkInFinishSave());
    }

    /**
     * 生成资产赔偿
     */
    public void checkInFinishSave(){

        String otherexplain = etOtherexplain.getText().toString().trim();
        String hygieneexplain = etHygieneexplain.getText().toString().trim();
        if(TextUtils.isEmpty(otherexplain)){
            MyUtils.showToast(etOtherexplain.getHint().toString());
            return;
        }
        if(TextUtils.isEmpty(hygieneexplain)){
            MyUtils.showToast(etHygieneexplain.getHint().toString());
            return;
        }

        showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid",  getIntent().getStringExtra(AppConst.BILLID));
            object.put("otherexplain",  otherexplain);
            object.put("hygieneexplain",  hygieneexplain);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkInFinishSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("资产赔偿生成成功!");

                setResult(RESULT_OK);
                finish();

            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
